package tech.aistar.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import tech.aistar.mapper.FlowerMapper;
import tech.aistar.pojo.Flower;
import tech.aistar.pojo.dto.FlowerQueryDTO;
import tech.aistar.service.FlowerService;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import tech.aistar.pojo.result.ResultReturn;

import java.util.List;

@RestController
@RequestMapping("/flower")
@Slf4j
public class FlowerController {

    @Autowired
    private FlowerService flowerService;



    @GetMapping
    public List<Flower> getFlower(FlowerQueryDTO flowerQueryDTO) throws UnsupportedEncodingException {
        // 若后端默认用ISO-8859-1解码，需手动转为UTF-8
        log.info("flowerQueryDTO:{}", flowerQueryDTO);

        // 添加参数校验，防止null参数传递给service层
        if (flowerQueryDTO == null) {
            flowerQueryDTO = new FlowerQueryDTO();
        }
        if (flowerQueryDTO.getPageNum() == null) {
            flowerQueryDTO.setPageNum(1);
        }
        if (flowerQueryDTO.getPageSize() == null) {
            flowerQueryDTO.setPageSize(20);
        }

        List<Flower> list = flowerService.findByOutherPage(
                flowerQueryDTO.getName(),
                flowerQueryDTO.getUse(),
                flowerQueryDTO.getMaterial(),
                flowerQueryDTO.getCategory(),
                flowerQueryDTO.getCount(),
                flowerQueryDTO.getPageNum(),
                flowerQueryDTO.getPageSize()
        ).getList();
        return list;
    }



    @GetMapping("/use/{use}")
    public ResultReturn<List<Flower>> findFlowerByUse(@PathVariable("use") String use) {
        List<Flower> flowers = flowerService.findByUse(use);
        return ResultReturn.success(flowers);
    }
    @GetMapping("/id/{id}")
    public ResultReturn<Flower> findFlowerById(@PathVariable("id") int id) {
        Flower flower=flowerService.findById(id);
        return ResultReturn.success(flower);
    }
}
